package com.xiaoluo.common.queue;

import com.xiaoluo.cd.entity.base.Jobs;
import com.xiaoluo.common.utils.ApplicationContextUtils;
import com.xiaoluo.common.utils.Tools;
import com.xiaoluo.web.form.ProjectForm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.*;

/**
 * Project : XL-CD
 * Package : com.xiaoluo.common.queue
 * Created by Willian.
 * Date  : 2016/1/6 20:51.
 */
public class JobMonitor implements Runnable{
    private final static Logger logger = LoggerFactory.getLogger(JobMonitor.class);

    private final static int MAX_THREADS = 30;

    private ThreadPoolExecutor executorService =  new ThreadPoolExecutor(MAX_THREADS, MAX_THREADS,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>());

    @Override
    public void run() {
        while (true){

            while (!JobQueue.queue.isEmpty()){

                if(executorService.getActiveCount()<MAX_THREADS){
                    Jobs obj = JobQueue.queue.poll();
                    logger.info("poll Job:{}",obj.getJobName());
                    executorService.execute(new JobWorker(obj));
                }

            }
            Tools.thinking(10);
        }
    }
}
